const isValid = function(s) {
    // 数组模拟栈
    let stack = [];

    for (let i = 0; i < s.length; i++) {
        if (s[i] == '(') {
            stack.push(')');
        } else if (s[i] == '[') {
            stack.push(']');
        } else if (s[i] == '{') {
            stack.push('}');
        } else {
            if (stack.length < 1 || stack.pop() != s[i]) {
                return false;
            }
        }
    }
    return stack.length == 0;
};

const s1 = "()";  // output: true
const s2 = "()[]{}";  // output: true
const s3 = "(]";  // output: false
const s4 = "([)]";  // output: false
const s5 = "{[]}";  // output: true

let inputs = [];
inputs.push(s1);
inputs.push(s2);
inputs.push(s3);
inputs.push(s4);
inputs.push(s5);

for (let i = 0; i < inputs.length; i++) {
    const res = isValid(inputs[i]);
    console.log(res);
}
